﻿function doClickAffair(prefix, left, oldposx, oldposy, newposx, newposy)
{
	var jewelId = "#" + prefix + "Jewel";
	var flyoutId = "#" + prefix + "sFlyout";
	var flyoutShow = false;
	
	$(document).click(function(event)
	{
		if(flyoutShow == false)
		{
			var offset = $(jewelId).offset();
			
			var flyoutXMin = offset.left;
			var flyoutYMin = offset.top;
			var flyoutXMax = flyoutXMin + $(jewelId).innerWidth();
			var flyoutYMax = flyoutYMin + $(jewelId).innerHeight();
			if((event.pageX >= flyoutXMin) && (event.pageX <= flyoutXMax)
			&& (event.pageY >= flyoutYMin) && (event.pageY <= flyoutYMax))
			{
				flyoutShow = true;
				$(flyoutId).css('left', left + 'px');
				$(jewelId + " a").css('background-position', newposx + "px " + newposy + "px");
				$(jewelId + " a").css('background-color', '#white');
				$(jewelId).css('border-left', '1px solid black');
				$(jewelId).css('border-right', '1px solid black');
				$(jewelId).css('border-top', '1px solid black');
				$(jewelId).css('border-bottom', '0px');
				
			}
		}
		else
		{
			var offset = $(flyoutId).offset();
			var flyoutXMin = offset.left;
			var flyoutYMin = offset.top;
			var flyoutXMax = flyoutXMin + $(flyoutId).innerWidth();
			var flyoutYMax = flyoutYMin + $(flyoutId).innerHeight();
			
			if((event.pageX < flyoutXMin) || (event.pageX > flyoutXMax) 
			|| (event.pageY < flyoutYMin) || (event.pageY > flyoutYMax))
			{
				flyoutShow = false;
				$(flyoutId).css('left', '-99999px');
				$(jewelId + " a").css('background-position', oldposx + "px " + oldposy + "px");
				$(jewelId + " a").css('background-color', '#3B5998');
				$(jewelId).css('border-left', '1px solid #3B5998');
				$(jewelId).css('border-right', '1px solid #3B5998');
				$(jewelId).css('border-top', '0px');
			}
		}
	});
	
	$(jewelId + " a").hover(function()
	{
		if(flyoutShow == true)
			return;
		$(jewelId + " a").css('background-color', '#4B67A1');
	},
	function()
	{
		if(flyoutShow == false)
		{
			$(jewelId + " a").css('background-color', '#3B5998');
		}
	});
		
}

function fixImage(image, scale)
{
	new_image = new Image();
	new_image.src = image.src;

	if(new_image.width > new_image.height)
	{
		new_image.height = new_image.height * scale / new_image.width;
		new_image.width = scale;
	}
	else
	{
		new_image.width = new_image.width * scale / new_image.height;
		new_image.height = scale;
	}
	image.width = new_image.width;
	image.height = new_image.height;
}

function fixImageWidth(image, scale)
{
	new_image = new Image();
	new_image.src = image.src;
	new_image.height = new_image.height * scale / new_image.width;
	new_image.width = scale;
	
	image.width = new_image.width;
	image.height = new_image.height;
}

$(document).ready(function()
{
	var profileShow = false;
	
	
	$(document).click(function(event)
	{
		if(profileShow == false)
		{
			var offset = $("#session").offset();
			var sessionXMin = offset.left;
			var sessionYMin = offset.top;
			var sessionXMax = sessionXMin + $("#session").innerWidth();
			var sessionYMax = sessionYMin + $("#session").innerHeight();
			if((event.pageX >= sessionXMin) && (event.pageX <= sessionXMax) 
		&& (event.pageY >= sessionYMin) && (event.pageY <= sessionYMax))
			{
				profileShow = true;
				$("#profileDropDown").css('left', '0px');
				$("#profileDropDown").css('top', '41px');
				$("#session").css('background-color', '#4B67A1');
			}
		}
			
		else
		{
			var offset = $("#profileDropDown").offset();
			var sessionXMin = offset.left;
			var sessionYMin = offset.top;
			var sessionXMax = sessionXMin + $("#profileDropDown").innerWidth();
			var sessionYMax = sessionYMin + $("#profileDropDown").innerHeight();
			
			if((event.pageX < sessionXMin) || (event.pageX > sessionXMax) 
			|| (event.pageY < sessionYMin) || (event.pageY > sessionYMax))
			{
				profileShow = false;
				$("#profileDropDown").css('left', '-9999px');
				$("#profileDropDown").css('top', '-9999px');
				$("#session").css('background-color', '#3B5998');
			}
		}
	});
	
	$("#session").hover(function()
	{
		$("#session").css('background-color', '#4B67A1');
	},
	function()
	{
		if(profileShow == false)
		{
			$("#session").css('background-color', '#3B5998');
		}
	});
	//doClickAffair("request", 0, -255, -230, -208, -270);
	//doClickAffair("message", 26, -267, -142, -233, -270);
	//doClickAffair("notification", 52, -280, -230, -258, -270);
	
	$(".uiListHorizontalItem a img").hover(function()
	{
		$(this).parent().children(".userHorizontalProfileName").css("visibility", "visible");
	},
	function()
	{
		$(this).parent().children(".userHorizontalProfileName").css("visibility", "hidden");
	});
	
});



